package com.app.tool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;

import com.jiangdg.usbcamera.utils.Lf_Debug;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.security.MessageDigest;

public class FilesUtil
{
	@SuppressLint("SdCardPath")
	private static String MYTMS_PATH_SDCARD_DIR = "/data/data/";
	private static String MYPACKAGE;
	private static String MYDIR;
	static final int SHA1_HASH_SIZE = 20;
	static final String LABLE_SHA1CHECK   = "CHECK_MODE:HASH";
	static final String LABLE_SHA256CHECK = "CHECK:SHA256";
	static final String LABLE_RSA1CHECK   = "CHECK:RSA1";
	static final String LABLE_RSA256CHECK = "CHECK:RSA256";

	static final String MAPP_FLAG = "S950_MAPP";
	static final String SAPP_FLAG = "S950_SAPP";
	/**
	 * 算法常量： SHA1
	 */
	private static final String ALGORITHM_SHA1 = "SHA-1";
	/**
	 * 算法常量： SHA256
	 */
	private static final String ALGORITHM_SHA256 = "SHA-256";
	
    public FilesUtil() {
    
    }
    
    public static void setPackageName(String name)
    {
    	MYPACKAGE = name;
    	MYDIR = "/file/";
    	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR);
    	File file = new File(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR);
        if (!file.exists()) {
            file.mkdir();
        }
    }
    
    public static String getPackageName()
    {
    	return MYPACKAGE;
    }
    
    public class APP_TAGINFO{
    	byte[] sha = new byte[32];
    	int shalen;
    	byte[] rsa = new byte[256];
    	int type;
    	int taglen;
    	int app_type;
    }
    
 // SHA1和SHA256,tag结构体 288B=272+16B=16+256+16B
    public class ST_taginfo
    {
    	byte[] version = new byte[16];   //
    	byte[] sha1 = new byte[32];      //上电
    	byte[] mac1 = new byte[16];
    	byte[] sha2 = new byte[32];      //运行 目前无用无效
    	byte[] mac2 = new byte[16];
    	byte[] reserved = new byte[160];
    	byte[] tag = new byte[16];       //"CHECK:SHA256"
    	
    	public void ST_taginfo_set(byte[] answer){
	    	int offset = 0;
	    	System.arraycopy(answer, offset, version, 0, version.length);
	    	Lf_Debug.dbg_mPrintfWHex(Lf_Debug._FILE_LINE_FUNC_(), version, version.length, "version");
	    	offset+=version.length;
	    	System.arraycopy(answer, offset, sha1, 0, sha1.length);
	    	Lf_Debug.dbg_mPrintfWHex(Lf_Debug._FILE_LINE_FUNC_(), sha1, sha1.length, "sha1");
	    	offset+=sha1.length;
	    	System.arraycopy(answer, offset, mac1, 0, mac1.length);
	    	offset+=mac1.length;
	    	System.arraycopy(answer, offset, sha2, 0, sha2.length);
	    	offset+=sha2.length;
	    	System.arraycopy(answer, offset, mac2, 0, mac2.length);
	    	offset+=mac2.length;
	    	System.arraycopy(answer, offset, reserved, 0, reserved.length);
	    	offset+=reserved.length;	    	
	    	System.arraycopy(answer, offset, tag, 0, tag.length);
	    	offset+=tag.length; 	
	    }
    	
    	public int length()
        {
    		int len = 0; //int型占用两个char的空间
    		
    		len+=version.length;
    		len+=sha1.length;
    		len+=mac1.length;
    		len+=sha2.length;
    		len+=mac2.length;
    		len+=reserved.length;
    		len+=tag.length;
 
    		return len;
        }
    };
    // RSA1和RSA256,tag结构体 640B=624+16B=16+48+48+256+256+16B
    public class ST_extaginfo
    {
    	byte[] version = new byte[16];   //
    	byte[] sha1 = new byte[32];      //上电SHA校验码明文
    	byte[] mac1 = new byte[16];      //目前无效
    	byte[] sha2 = new byte[32];      //运行SHA校验码明文，目前无效
    	byte[] mac2 = new byte[16];      //目前无效
    	byte[] RSA1 = new byte[256];     //上电检测SHA校验码密文，用公钥加密
    	byte[] RSA2 = new byte[256];     //运行检测SHA校验码密文，用公钥加密，目前无效
    	byte[] tag = new byte[16];       //"CHECK:RSA1"
    	
    	public void ST_extaginfo_set(byte[] answer){
	    	int offset = 0;
	    	System.arraycopy(answer, offset, version, 0, version.length);
	    	offset+=version.length;
	    	System.arraycopy(answer, offset, sha1, 0, sha1.length);
	    	offset+=sha1.length;
	    	System.arraycopy(answer, offset, mac1, 0, mac1.length);
	    	offset+=mac1.length;
	    	System.arraycopy(answer, offset, sha2, 0, sha2.length);
	    	offset+=sha2.length;
	    	System.arraycopy(answer, offset, mac2, 0, mac2.length);
	    	offset+=mac2.length;
	    	System.arraycopy(answer, offset, RSA1, 0, RSA1.length);
	    	offset+=RSA1.length;	   
	    	System.arraycopy(answer, offset, RSA2, 0, RSA2.length);
	    	offset+=RSA2.length;	
	    	System.arraycopy(answer, offset, tag, 0, tag.length);
	    	offset+=tag.length; 	
	    }
    	
    	public int length()
        {
    		int len = 0; //int型占用两个char的空间
    		
    		len+=version.length;
    		len+=sha1.length;
    		len+=mac1.length;
    		len+=sha2.length;
    		len+=mac2.length;
    		len+=RSA1.length;
    		len+=RSA2.length;
    		len+=tag.length;
    
    		return len;
        }
    	
    	
    };
    
    public class APP_INFO_T
    {
        public byte[] AppName = new byte[32];
        public byte[] AppVer = new byte[16];
        public byte[] AppProvider = new byte[32];
        public byte[] BuildTime = new byte[14];
        public byte[] Descript = new byte[64];
        public int AppNo;
        
        public void APP_INFO_T_set(byte[] answer){
	    	int offset = 0;
	    	System.arraycopy(answer, offset, AppName, 0, AppName.length);
	    	offset+=AppName.length;
	    	System.arraycopy(answer, offset, AppVer, 0, AppVer.length);
	    	offset+=AppVer.length;
	    	System.arraycopy(answer, offset, AppProvider, 0, AppProvider.length);
	    	offset+=AppProvider.length;
	    	System.arraycopy(answer, offset, BuildTime, 0, BuildTime.length);
	    	offset+=BuildTime.length;
	    	System.arraycopy(answer, offset, Descript, 0, Descript.length);
	    	offset+=Descript.length;
	    	AppNo = (int)(answer[offset]&0xff) + (int)((answer[offset+1]&0xff)<<8) + (int)((answer[offset+2]&0xff)<<16) + (int)((answer[offset+3]&0xff)<<24);
	    	offset+=4;
	    }
        
        public int length()
        {
    		int len = 0; //int型占用两个char的空间
    		
    		len+=AppName.length;
    		len+=AppVer.length;
    		len+=AppProvider.length;
    		len+=BuildTime.length;
    		len+=Descript.length;
    		len+=4;
    
    		return len;
        }
    }
    
//    public APP_INFO_T bintag_file_version(String filename)
//    {
//    	FilesUtil fileutil = new FilesUtil();
//    	APP_INFO_T appinfo = fileutil.new APP_INFO_T();
//    	byte[] table = new byte[3];
//    	if(existFile(filename))
//    	{
//    		table = readFile(filename, 0, 3);
//    		if(Arrays.areEqual(table, "zip".getBytes()))
//    		{
//    			byte[] app_info = new byte[appinfo.length()];
//    			app_info = readFile(filename, 3, app_info.length);
//    			appinfo.APP_INFO_T_set(app_info);
//    			return appinfo;
//    		}
//    		else
//    		{
//    			int offset=0;
//    			byte[] app_info = new byte[appinfo.length()];
//    			table = readFile(filename, 4, 3);
//    			offset = (int)(table[0]&0xff) + (int)((table[1]&0xff)<<8) + (int)((table[2]&0xff)<<16);
//    			offset *=4;
//    			offset +=12;
//    			app_info = readFile(filename, offset, app_info.length);
//    			appinfo.APP_INFO_T_set(app_info);
//    			return appinfo;
//    		}
//    	}
//    	return null;
//    }
    
    public static byte[] bintag_file_sha256(String filename, APP_TAGINFO taginfo)
    {
    	int offset;
    	int len,filesize;
    	
    	offset = 0;
		byte[] filedata = new byte[8*1024];
		if(existFile(filename))
    	{
    		filesize = (int)sizeFile(filename);
    	}
		else
		{
			return null;
		}
		MessageDigest md = null;
		if(taginfo.shalen == SHA1_HASH_SIZE)
		{
			Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "SHA1");
			try {    					
				md = MessageDigest.getInstance(ALGORITHM_SHA1);
			} catch (Exception e) {
				Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
				return null;
			}				
		}
		else
		{
			Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "SHA256");
			try {    					
				md = MessageDigest.getInstance(ALGORITHM_SHA256);
			} catch (Exception e) {
				Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
				return null;
			}  				
		}
		md.reset();		
		filesize-=taginfo.taglen;
		Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "%d %d", filesize, taginfo.taglen);
		while(true)
		{
			if(filesize - offset < filedata.length)
			{
				len = filesize - offset;
			}
			else
			{
				len = filedata.length;
			}			
			filedata = readFile(filename, offset, len);
			//Lf_Debug.dbg_mPrintfWHex(Lf_Debug._FILE_LINE_FUNC_(), filedata, filedata.length, "offset %d filedata %d", offset, len);
			offset += len;								
			try {    					
				md.update(filedata);				
			} catch (Exception e) {
				Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
				return null;
			}
			
			if(offset >= filesize)
			{
				break;
			}
		} 			
		return md.digest();
    }
//
//    public static boolean bintag_file_parse(String filename)
//    {
//    	int len,filesize;
//
//    	byte[] table = new byte[4096];
//    	byte[] taglable = new byte[16];
//    	byte[] apptype = new byte[16];
//    	FilesUtil fileutil = new FilesUtil();
//    	APP_TAGINFO taginfo = fileutil.new APP_TAGINFO();
//    	ST_taginfo st_taginfo;
//    	ST_extaginfo st_extaginfo;
//    	if(existFile(filename))
//    	{
//    		filesize = (int)sizeFile(filename);
//    		if(filesize>= table.length)
//    		{
//    			len = table.length;
//    			table = readFile(filename, filesize-len, len);
//    			Lf_Debug.dbg_mPrintfWHex(Lf_Debug._FILE_LINE_FUNC_(), table, table.length, "tag");
//    			System.arraycopy(table, table.length-taglable.length, taglable, 0, taglable.length);
//    			String tag = new String(taglable);
//    			Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), tag);
//    			if(LABLE_SHA1CHECK.equals(tag.substring(0, LABLE_SHA1CHECK.length())))
//    			{
//    				taginfo.type = 0;
//    				taginfo.shalen = SHA1_HASH_SIZE;
//    				st_taginfo = fileutil.new ST_taginfo();
//    				byte[] stinfo = new byte[st_taginfo.length()];
//    				System.arraycopy(table, table.length-stinfo.length, stinfo, 0, stinfo.length);
//    				st_taginfo.ST_taginfo_set(stinfo);
//    				System.arraycopy(st_taginfo.sha1, 0, taginfo.sha, 0, taginfo.shalen);
//    				System.arraycopy(st_taginfo.version, 0, apptype, 0, apptype.length);
//    				taginfo.taglen = stinfo.length - 16;
//    			}
//    			else if(LABLE_SHA256CHECK.equals(tag.substring(0, LABLE_SHA256CHECK.length())))
//    			{
//    				taginfo.type = 1;
//    				taginfo.shalen = 32;
//    				st_taginfo = fileutil.new ST_taginfo();
//    				byte[] stinfo = new byte[st_taginfo.length()];
//    				System.arraycopy(table, table.length-stinfo.length, stinfo, 0, stinfo.length);
//    				st_taginfo.ST_taginfo_set(stinfo);
//    				System.arraycopy(st_taginfo.sha1, 0, taginfo.sha, 0, taginfo.shalen);
//    				System.arraycopy(st_taginfo.version, 0, apptype, 0, apptype.length);
//    				taginfo.taglen = stinfo.length - 16;
//
//    			}
//    			else if(LABLE_RSA1CHECK.equals(tag.substring(0, LABLE_RSA1CHECK.length())))
//    			{
//    				taginfo.type = 2;
//    				taginfo.shalen = SHA1_HASH_SIZE;
//    				st_extaginfo = fileutil.new ST_extaginfo();
//    				byte[] exstinfo = new byte[st_extaginfo.length()];
//    				System.arraycopy(table, table.length-exstinfo.length, exstinfo, 0, exstinfo.length);
//    				st_extaginfo.ST_extaginfo_set(exstinfo);
//    				System.arraycopy(st_extaginfo.RSA1, 0, taginfo.rsa, 0, 256);
//    				System.arraycopy(st_extaginfo.sha1, 0, taginfo.sha, 0, taginfo.shalen);
//    				System.arraycopy(st_extaginfo.version, 0, apptype, 0, apptype.length);
//    				taginfo.taglen = exstinfo.length - 16;
//    			}
//    			else if(LABLE_RSA256CHECK.equals(tag.substring(0, LABLE_RSA256CHECK.length())))
//    			{
//    				taginfo.type = 3;
//    				taginfo.shalen = 32;
//    				st_extaginfo = fileutil.new ST_extaginfo();
//    				byte[] exstinfo = new byte[st_extaginfo.length()];
//    				System.arraycopy(table, table.length-exstinfo.length, exstinfo, 0, exstinfo.length);
//    				st_extaginfo.ST_extaginfo_set(exstinfo);
//    				System.arraycopy(st_extaginfo.RSA1, 0, taginfo.rsa, 0, 256);
//    				System.arraycopy(st_extaginfo.sha1, 0, taginfo.sha, 0, taginfo.shalen);
//    				System.arraycopy(st_extaginfo.version, 0, apptype, 0, apptype.length);
//    				taginfo.taglen = exstinfo.length - 16;
//    			}
//    			else
//    			{
//    				Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "文件破损");
//    				return false;
//    			}
//    			Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), new String(apptype));
//    			if(MAPP_FLAG.equals(new String(apptype).substring(0, MAPP_FLAG.length())))
//    			{
//    				taginfo.app_type = 0;
//    			}
//    			else if(SAPP_FLAG.equals(new String(apptype).substring(0, SAPP_FLAG.length())))
//    			{
//    				taginfo.app_type = 1;
//    			}
//    			else
//    			{
//    				Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "未知应用");
//    				return false;
//    			}
//
//    			byte[] calcsha = bintag_file_sha256(filename, taginfo);
//    			Lf_Debug.dbg_mPrintfWHex(Lf_Debug._FILE_LINE_FUNC_(), calcsha, calcsha.length, "last calcsha");
//    			Lf_Debug.dbg_mPrintfWHex(Lf_Debug._FILE_LINE_FUNC_(), taginfo.sha, taginfo.sha.length, "SHA1");
//    			if(Arrays.areEqual(taginfo.sha, calcsha))
//    			{
//    				return true;
//    			}
//    		}
//    	}
//    	return false;
//    }
    
    public static void createFile(String filename)
    {
    	File file = new File(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR, filename);
    	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR);
    	if(!file.exists())
    	{ 		
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
			}   				
    	}
    }
    
    public static void removeFile(String filename)
    {
    	File file = new File(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR, filename);  
    	if(file.exists())
    	{
    		file.delete();
    	}
    }
    
    public static boolean existFile(String filename)
    {
    	File file = new File(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR, filename);
    	if (file.exists()) 
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    public static long sizeFile(String filename)
    {
    	File file = new File(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR, filename);  
    	if (file.exists()) 
    	{
    		return file.length();
    	}
    	else
    	{
    		return 0;
    	}
    }
    
    public static byte[] readFile(String filename, int staroffset, int datalen)
    {
    	byte[] data = new byte[datalen];
    	File file = new File(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR, filename);
    	if (file.exists()) 
    	{    		
			try {
				RandomAccessFile afile = new RandomAccessFile(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR+filename,"rw");
				afile.seek(staroffset);
				afile.read(data);				
				afile.close();
			} 
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
				return null;
			}    			    	    
    	}
    	return data;
    }
    
    public static void writeFile(String filename, int staroffset, byte[] data)
    {
    	File file = new File(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR, filename);  
    	if (file.exists()) 
    	{   		
			try {
				RandomAccessFile afile = new RandomAccessFile(MYTMS_PATH_SDCARD_DIR+MYPACKAGE+MYDIR+filename,"rw");				
				afile.seek(staroffset);				
				afile.write(data);
				afile.close();
			} 
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
			}    			    	    
    	}
    }
    
    public static void makeFileDir(String fileDir)
    {
        File file = new File(fileDir);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public static File getFile(String fileDir, String fileName)
    {
        File file = new File(fileDir, fileName);

        if (file.exists()) {
            file.delete();
        }
        try
        {
            file.createNewFile();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
            throw new RuntimeException("文件创建失败");
        }
        return file;
    }
    
    public static String getFileDir(Context context)
    {
        if ("mounted".equals(Environment.getExternalStorageState())) {
            return Environment.getExternalStorageDirectory() + "/";
        }
        return context.getFilesDir() + "/";
    }
    
    public static boolean isApkFile(File file)
    {
        if ((file != null) && (file.getName() != null)) {
            String fileName = file.getName();
            return (fileName.endsWith(".APK")) || (fileName.endsWith(".apk"));
        }
        return false;
    }

    public static boolean isUnsOrBinFile(File file) {
        if ((file != null) && (file.getName() != null)) {
            String fileName = file.getName();
            return (fileName.endsWith(".UNS")) || (fileName.endsWith(".uns")) || (fileName.endsWith(".BIN")) || (fileName.endsWith(".bin")) || (fileName.endsWith(".NLP")) || (fileName.endsWith(".nlp"));
        }
        return false;
    }

    public static int copy(String fromFile, String toFile)
    {
        File root = new File(fromFile);
        if (!root.exists())
        {
            return -1;
        }
        File[] currentFiles = root.listFiles();
        File targetDir = new File(toFile);
        if (!targetDir.exists())
        {
            targetDir.mkdirs();
        }
        for (int i = 0; i < currentFiles.length; i++)
        {
            if (currentFiles[i].isDirectory())
            {
                 copy(currentFiles[i].getPath() + "/", toFile + currentFiles[i].getName() + "/");
            }
            else
            {
                 CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName());
            }
        }
        return 0;
    }

    public static int CopySdcardFile(String fromFile, String toFile)
    {
        try
        {
            InputStream fosfrom = new FileInputStream(fromFile);
            OutputStream fosto = new FileOutputStream(toFile);
            byte[] bt = new byte['?'];
            int c;
            while ((c = fosfrom.read(bt)) > 0) 
            {
                fosto.write(bt, 0, c);
            }
            fosfrom.close();
            fosto.close();
            return 0;
        }
        catch (Exception e)
        {
            e.printStackTrace(); 
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return -1;
    }
    
    public static boolean copyFile(String oldPath, String newPath) {
        InputStream inStream = null;
        OutputStream outStream = null;
        try {
            int bytesum = 0;
            int byteread = 0;
            long total = 1;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) 
            { //文件存在时
            	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "copy["+oldPath+"]to["+newPath+"]...");
                total = oldfile.length();
                Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "total:%d", (int)total);                
                File newfile = new File(newPath);    
                if(total == newfile.length())
                {
                	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "the same file");
                	return true;
                }
                if(newfile.exists())
                {               	
                	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), newPath+" exist");
                	if(newfile.delete())
                	{
                		Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), newPath+" del");
                	}
                }
                inStream = new FileInputStream(oldfile); //读入原文件                               
                outStream = new FileOutputStream(newfile);                
                if (outStream != null && inStream != null)
                {
                    byte[] buffer = null;
                    if (total < (1024 * 50)) 
                    {
                        buffer = new byte[1024];
                    } 
                    else if (total < (1024 * 500)) 
                    {
                        buffer = new byte[1024 * 10];
                    } 
                    else if (total < (1024 * 5000)) 
                    {
                        buffer = new byte[1024 * 100];
                    } 
                    else 
                    {
                        buffer = new byte[1024 * 500];
                    }
                    while ((byteread = inStream.read(buffer)) != -1) 
                    {
                    	//Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(DEBUG_LEVEL), "byteread:%d bytesum:%d", byteread, bytesum);
                        bytesum += byteread; //字节数 文件大小                                               
                        outStream.write(buffer, 0, byteread);
                        outStream.flush();
                        if(bytesum >= total)
                        {                        	
                        	break;
                        }
                    }
                }
                if (inStream != null)
                {
                    inStream.close();
                }
                if (outStream != null)
                {
                	outStream.close();
                }
                File savefile = new File(newPath);     
                if(savefile.exists())
                {
                	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "save:%d", (int)savefile.length());
                }
            }
           
        } 
        catch (Exception e)
        {
        	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "copy["+oldPath+"]err...");
            e.printStackTrace();
            try {
                if (inStream != null)
                {
                    inStream.close();
                }
                if (outStream != null)
                {
                	outStream.close();
                }
            } 
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
            return false;
        }
        return true;
    }
	
    @SuppressLint("SdCardPath")
	public static void create_productconfig(String filename)
	{
		Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "create_config");
		JSONObject object = new JSONObject();
	    //string
	    try {
			object.put("产品型号","LF-285");
			object.put("内核版本","3.10.49");
		    object.put("系统版本","LF285_V1.00.009");
		    object.put("生产版本","1.1.97-20190412");
		    object.put("应用版本","1.1.9a");
		    object.put("读卡器版本","20190314");
		    
		    object.put("授权码","FA2E27EF9451B1BDA70401137DC35AB484E0EDEDB33075FD0000000000000000");		    
		    object.put("序列号","20190314");
		    
		    object.put("内核升级","0");
		    object.put("内核升级路径","0");
		    object.put("系统升级","0");
		    object.put("系统升级路径","0");		   
		    object.put("生产升级","0");
		    object.put("生产升级路径","0");
		    object.put("应用升级","0");
		    object.put("应用升级路径","0");
		    object.put("读卡器升级","0");
		    object.put("读卡器升级路径","0");
		    
		    object.put("授权码升级","0");
		    object.put("序列号升级","0");
		    
		    object.put("校验码SHA256","XXXXXXXX");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	    
	    String json=object.toString();
	    
	    String databakpath = "/sdcard/logs/";
    	Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "save path:"+databakpath);
    	File savefile = new File(databakpath);
        if (!savefile.exists()) {
        	savefile.mkdir();
        }
        OutputStream outStream = null;
        String copyfile = filename;
        Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "copyfile:"+copyfile);
		File dbfile = new File(databakpath+copyfile);
		if (savefile.exists()) {
			dbfile.delete();
		}
		
	    try {
			outStream = new FileOutputStream(dbfile);               
			byte[] buffer = json.getBytes();
			outStream.write(buffer, 0, buffer.length);
	        outStream.flush();
	        if (outStream != null)
            {
            	outStream.close();
            }
		}
		catch (Exception e)
        {
            e.printStackTrace();
            try {                
                if (outStream != null)
                {
                	outStream.close();
                }
            } 
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
        }
	}
}
